import { ref, onMounted } from 'vue'
import { getImageApi } from '@/api/user/user'

// 测试专用。当手动切换到首页的时候,需要清空token再请求图片验证码
import { setToken } from '@/utils/auth'

export default function useImage() {
    // 定义图片的
    const imgSrc = ref('')
    // 获取图片  btoa:创建一个base64编码的字符串
    const getImg = async () => {
        await getImageApi().then(res => {
            // console.log(res)
            return 'data:image/png;base64,' + window.btoa(
                new Uint8Array(res.data as any).reduce((data,byte) => data + String.fromCharCode(byte), '')
            )
        }).then(data => {
            // console.log(data)
            imgSrc.value = data
        })
    }

    onMounted(() => {
        getImg()
        setToken('')
    })

    return {
        imgSrc,
        getImg
    }
}